package com.yidu.hbatsft.action;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.hbatsft.domain.*;
import com.yidu.hbatsft.service.AtsBaseSportService;
import com.yidu.hbatsft.service.AtsEmpCoachService;
import com.yidu.hbatsft.service.AtsPlanItemService;
import com.yidu.hbatsft.service.AtsPlanYearService;
import com.yidu.privilege.Utils.BaseMessage;
import com.yidu.utils.FileTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * 3.年度计划
 * @author 龙兴
 * @time 2019年10月31日
 */
@Controller
@RequestMapping("/atsYear")
public class AtsPlanYearController {
    @Resource
    AtsPlanYearService atsPlanYearService;//年度计划

    @Resource
    AtsBaseSportService atsBaseSportService;// 体育项目管理

    @Resource
    AtsEmpCoachService atsEmpCoachService;// 教练员

    @Resource
    AtsPlanItemService atsPlanItemService;//指标

    @Resource
    FileTools fileTools;//文件上传

    /**
     * 查询所有
     * @param pageNum 当前所在页数
     * @param pageSize 总页数
     * @param atsPlandimSearch 模糊查询所需要的值
     * @return 查询的结果
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public Map<String,Object> selectAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize, AtsPlandimSearch atsPlandimSearch){
        Map<String, Object> map = new HashMap<String, Object>();
        List<AtsPlanYear> list = atsPlanYearService.selectAll(pageNum,pageSize,atsPlandimSearch);//分页数据
        Integer count = atsPlanYearService.count(atsPlandimSearch);// 分页插件需要的总数

        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data",list);

        return map;
    }

    /**
     * 用户穿梭树和下拉框的查询
     * @return 查询的结果
     */
    @RequestMapping("selMessage")
    @ResponseBody
    public Map<String,Object> selMessage(){

        Map<String, Object> map = new HashMap<String, Object>();
        //所有运动项目名
        List<AtsBaseSport> atsBaseSport = atsBaseSportService.selAllSportNmae();
        //所有教练员的姓名
        List<AtsEmpCoach> atsEmpCoach = atsEmpCoachService.selAllCoachName();
        //指标
        List<AtsPlanItem> atsPlanItem = atsPlanItemService.selectItem("2");

        map.put("sport",atsBaseSport);
        map.put("coach",atsEmpCoach);
        map.put("item",atsPlanItem);

        return map;
    }

    /**
     * 新增或修改
     * @param atsPlanYear 年度计划
     * @param atsPlanItem 指标
     * @param itemIdArry 获取到指标的ID
     * @param file1s 文件1
     * @param file2s 文件2
     * @param file3s 文件3
     * @return
     */
    @RequestMapping("addOrUpdate")
    @ResponseBody
    public BaseMessage addOrUpdate(AtsPlanYear atsPlanYear, AtsPlanItem atsPlanItem, String itemIdArry, MultipartFile file1s, MultipartFile file2s, MultipartFile file3s){

        BaseMessage bm = new BaseMessage();

        atsPlanYear.setCreatTime(new Date());//设置创建时间
        atsPlanYear.setCreater("sb");//设置当前登陆人

        if (!"".equals(file1s.getOriginalFilename()) && file1s.getOriginalFilename() != null){
            try {
                String file1 = fileTools.saveFile("images",file1s);
                atsPlanYear.setFile1(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            atsPlanYear.setFile1(null);
        }
        if (!"".equals(file2s.getOriginalFilename()) && file2s.getOriginalFilename() != null){
            try {
                String file2 = fileTools.saveFile("images",file2s);
                atsPlanYear.setFile2(file2);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            atsPlanYear.setFile2(null);
        }
        if (!"".equals(file3s.getOriginalFilename()) && file3s.getOriginalFilename() != null){
            try {
                String file3 = fileTools.saveFile("images",file3s);
                atsPlanYear.setFile3(file3);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            atsPlanYear.setFile3(null);
        }

        ObjectMapper mapper = new ObjectMapper();
        String rs = null;
        try {
            rs = mapper.writeValueAsString(atsPlanItem);
            System.out.println(rs);
        } catch (Exception e) {
            System.out.println(123);
        }

        atsPlanItemService.itemUpdte("2");//将上一次指标都改为0
        // //通过SportId查询出atsPlanYearMore的item名
        AtsBaseSport atsBaseSport = atsBaseSportService.selSportNmaeLX(atsPlanYear.getSportId());
        //设置体育项目名称
        atsPlanYear.setItemName(atsBaseSport.getSportName());

        Integer temp = null;
        // 判断用于分辨是新增还是修改
        if(atsPlanYear.getPid() != 0){
            // 修改
            temp = atsPlanYearService.updateByPrimaryKeySelective(atsPlanYear,atsPlanItem,itemIdArry);
        }else{
            // 新增
            temp = atsPlanYearService.insertSelective(atsPlanYear,atsPlanItem);
        }
        if(temp > 0){
            bm.setState(1);
            bm.setMsg("操作成功");
        }else {
            bm.setState(0);
            bm.setMsg("操作失败");
        }
        return bm;
    }

    /**
     * 修改的查询
     * @param pid pid
     * @return 查询的结果集
     */
    @RequestMapping("updSel")
    @ResponseBody
    public Map<String,Object> selectByPrimaryKey(Integer pid){
        AtsPlanYear atsPlanYear = null;//定义多年年度计划
        List<AtsPlanItem> atsPlanItem = null;//定义atsPlanItem指标
        Map<String, Object> map = null;//定义hashmap集合
        //判断pid是否为空
        if (pid != null && !"".equals(pid)){
            map = new HashMap<String, Object>();//new出hashmap集合
            atsPlanYear = atsPlanYearService.selectByPrimaryKey(pid);//调用查询方法
            atsPlanItem = atsPlanItemService.selectItemByPid(pid.toString(),"2");//根据pid查询指标
            List<String> list = null;//定义list集合用于装教练员ID
            if(atsPlanYear.getOtherCoach() != null && !"".equals(atsPlanYear.getOtherCoach())) {//判断其他教练中是否有人
                String otherNameTemp[] = atsPlanYear.getOtherCoach().split(",");//通过“,”拆分字符串
                list = new ArrayList<String>();
                if (otherNameTemp[0] != null && !"".equals(otherNameTemp[0])) {//判断数组中是否为空
                    for (int i = 0; i < otherNameTemp.length; i++) {//循环otherNameTemp[]
                        String otherName = otherNameTemp[i];//取otherNameTemp每一次循环的值
                        AtsEmpCoach atsEmpCoach = atsEmpCoachService.selCoachId(otherName);//调用atsEmpCoachService中用于查询id的方法
                        list.add(atsEmpCoach.getCoachId().toString());//将coachId装到list集合中
                    }
                } else {
                    list.add(null);
                }
            }
            map.put("YearMore",atsPlanYear);
            map.put("Item",atsPlanItem);
            map.put("otherNameId",list);
        }

        return map;
    }

    /**
     * 删除
     * @param ids 删除所需的ID
     * @return
     */
    @RequestMapping("Delete")
    @ResponseBody
    public BaseMessage updateDelete(String ids){
        BaseMessage bm = new BaseMessage();
        Integer temp = atsPlanYearService.updateDelete(ids);
        if(temp > 0){
            bm.setState(1);
            bm.setMsg("删除成功");
        }else {
            bm.setState(0);
            bm.setMsg("删除失败");
        }
        return bm;
    }

}
